#include <iostream>
#include <functional>
#include "connection.hpp"
#include "Reactor.hpp"
#include "Listener.hpp"
#include "NormalConnectHander.hpp"
#include "PackagePrase.hpp"
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage:" << argv[0] << " serverport" << std::endl;
        exit(0);
    }
    uint16_t port = std::stoi(argv[1]);
    NetCal cal;
    PackagePrasse P(cal);

    Listener listener(port);                                                                 // 专门处理listen的连接
    HandlerConnection handers(std::bind(&PackagePrasse::Excute, &P, std::placeholders::_1)); // 处理普通IO的连接

    std::unique_ptr<Reactor> R = std::make_unique<Reactor>();

    R->SetOnConnect(std::bind(&Listener::Accepter, &listener, std::placeholders::_1));
    R->SetOnNormalHandler(
        std::bind(&HandlerConnection::HandlerRecver, &handers, std::placeholders::_1),
        std::bind(&HandlerConnection::HandlerSender, &handers, std::placeholders::_1),
        std::bind(&HandlerConnection::HanlderExcepter, &handers, std::placeholders::_1));

    R->AddConnection(listener.Listenfd(), EPOLLIN | EPOLLET, ListenConnection);
    R->Distach();
    return 0;
}